package com.pubinfo.passbook.common.model.dto.open;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.pubinfo.passbook.common.entity.TContractProjectNotClear;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 报账挑对记录表
 *
 * @TableName t_witeoff_open_picking
 */
@Data
public class WiteoffOpenPicking implements Serializable {
  
  /**
   * sap凭证号
   */
  @ApiModelProperty("sap凭证号")
  private String sapCertificateCode;
  
  /**
   * 凭证行项号
   */
  @ApiModelProperty("凭证行项号")
  private String sapItemMun;
  
  /**
   * 年份
   */
  @ApiModelProperty("年度")
  private String tYear;
  
  /**
   * 公司代码
   */
  @ApiModelProperty(value = "公司代码", hidden = true)
  private String companyCode = "A011";
  
  /**
   * 科目编码	非必须	供应商、客户、总账
   */
  @ApiModelProperty(value = "科目编码 非必须 供应商、客户、总账", hidden = true)
  private String accountCode = "G000343924";
  
  /**
   * 科目类型	非必须	1-总账、2-供应商、3-客户
   */
  @ApiModelProperty(value = "科目类型 非必须 1-总账、2-供应商、3-客户", hidden = true)
  private Byte accountType = 2;
  
  /**
   * 利润中心组
   */
  @ApiModelProperty("利润中心组")
  private String profitCenterGroupCode;
  
  /**
   * 外围系统子单Id	非必须	当业务场景是列账后付款时才是必须
   */
  @ApiModelProperty(value = "外围系统子单Id 非必须 当业务场景是列账后付款时才是必须", hidden = true)
  private String otherSystemDetailId;
  
  /**
   * 挑对金额	 	当业务场景是1：冲前期借款（预付款）和3：冲预列时才是必须
   */
  @ApiModelProperty("挑对金额   当业务场景是1：冲前期借款（预付款）和3：冲预列时才是必须")
  private BigDecimal pickingSum;
  /**
   * 报账金额	非必须	当业务场景是列账后付款时才是必须
   */
  @ApiModelProperty("报账金额   当业务场景是列账后付款时才是必须")
  private BigDecimal writeoffSum;
  
  /**
   * 进项税金额	非必须	当“票据类型”为1、4、5、6、11时，进项税金额必须
   */
  @ApiModelProperty("进项税金额   当“票据类型”为1、4、5、6、11时，进项税金额必须")
  private BigDecimal inputTaxSum;
  
  /**
   * 是否最后一次报账	非必须	当业务场景是列账后付款，且是最后一次报账时必须 1-是，0或空-否
   */
  @ApiModelProperty("是否最后一次报账   当业务场景是列账后付款，且是最后一次报账时必须 1-是，0或空-否")
  private Byte isLastWriteoff;
  
  /**
   * 报账单id
   */
  @ApiModelProperty(hidden = true)
  private String otherSystemMainId;
  
  @JacksonXmlProperty(localName = "tYear")
  public String gettYear() {
    return tYear;
  }
  
  @TableField(exist = false)
  private static final long serialVersionUID = 1L;

  public WiteoffOpenPicking() {
  }

  public WiteoffOpenPicking(TContractProjectNotClear notClear, String otherSystemDetailId) {
    tYear = notClear.getYear();
    companyCode = notClear.getCompayCode();
    this.otherSystemDetailId = otherSystemDetailId;
    this.sapCertificateCode = notClear.getCertificateCode();
    this.sapItemMun = notClear.getCertificateLine();
    String profitCenter = notClear.getProfitCenter();
    if (StringUtils.isNotBlank(profitCenter)) {
      this.profitCenterGroupCode = profitCenter.substring(0, 4);
    }
    this.writeoffSum = notClear.getAmount();
  }
  
  @JsonIgnore
  public static List<WiteoffOpenPicking> toList(List<TContractProjectNotClear> notClears, String otherSystemDetailId) {
    List<WiteoffOpenPicking> openPickings = new ArrayList<>();
    if (!CollectionUtils.isEmpty(notClears)) {
      notClears.stream().forEach(notClear -> {
        WiteoffOpenPicking openPicking = new WiteoffOpenPicking(notClear, otherSystemDetailId);
        openPickings.add(openPicking);
      });
    }
    return openPickings;
  }
}